// pages/goodsxq/goodsxq.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabindex: '1',     //tab切换index
    id: 'maodian1',   //初始锚点id
    pageheight: '',  //滑动页面高
    sizexzbox: false,  //判断弹出框动画class
    blackzt: 'none',  //黑幕层显示/隐藏状态
    goodsid:'',
    type:'',  //0普通 1满减 2满送 3限时 4拼团
    obj:{},
    actEndTimeList: [],
    countDownList:[],
    timer:'',
    pingjialist: [], //评价列表
    youxuanlist:[],
    haohuolist:[],
    guigemoney: '',
    sublist:[],
    guigestr:'',
    isOverShare: true,
    shareimg:'',
    img1:'',
    img2:'',
    suretype:''

  },
  topingjialist() {
    var goodsid = this.data.goodsid;
    wx.navigateTo({
      url: '../pingjialist/pingjialist?goodsid=' + goodsid,
    })
  },
  getimg1(imgurl){
    
    var _this = this;
    wx.downloadFile({
      url: imgurl, //仅为示例，并非真实的资源
      success(res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          // console.log(res.tempFilePath)
          _this.data.img1 = res.tempFilePath;
          _this.setData({
            img1: res.tempFilePath,
          })
        }
      }
    })
  },
  getimg2(imgurl) {
    // console.log(imgurl)
    var _this = this;
    wx.downloadFile({
      url: imgurl, //仅为示例，并非真实的资源
      success(res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          _this.data.img2 = res.tempFilePath;
          _this.setData({
            img2: res.tempFilePath,
          })
        }
      }
    })
  },
  roundRect(ctx, x, y, w, h, r, fillColor, strokeColor) {
    // 画圆角 ctx、x起点、y起点、w宽度、y高度、r圆角半径、fillColor填充颜色、strokeColor边框颜色
    // 开始绘制
    ctx.beginPath()

    // 绘制左上角圆弧 Math.PI = 180度
    // 圆心x起点、圆心y起点、半径、以3点钟方向顺时针旋转后确认的起始弧度、以3点钟方向顺时针旋转后确认的终止弧度
    ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5)

    // 绘制border-top
    // 移动起点位置 x终点、y终点
    ctx.moveTo(x + r, y)
    // 画一条线 x终点、y终点
    ctx.lineTo(x + w - r, y)
    // ctx.lineTo(x + w, y + r)

    // 绘制右上角圆弧
    ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2)

    // 绘制border-right
    ctx.lineTo(x + w, y + h - r)
    // ctx.lineTo(x + w - r, y + h)

    // 绘制右下角圆弧
    ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5)

    // 绘制border-bottom
    ctx.lineTo(x + r, y + h)
    // ctx.lineTo(x, y + h - r)

    // 绘制左下角圆弧
    ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI)

    // 绘制border-left
    ctx.lineTo(x, y + r)
    // ctx.lineTo(x + r, y)

    if (fillColor) {
      // 因为边缘描边存在锯齿，最好指定使用 transparent 填充
      ctx.setFillStyle(fillColor)
      // 对绘画区域填充
      ctx.fill()
    }

    if (strokeColor) {
      // 因为边缘描边存在锯齿，最好指定使用 transparent 填充
      ctx.setStrokeStyle(strokeColor)
      // 画出当前路径的边框
      ctx.stroke()
    }
    // 关闭一个路径
    // ctx.closePath()

    // 剪切，剪切之后的绘画绘制剪切区域内进行，需要save与restore
    ctx.clip()
  },
  getshareimg() {
    var _this = this;
    var type = this.data.type;
    var info = this.data.obj;
    var imglist = info.goodsImg;
    const ctx = wx.createCanvasContext('shareimg')
    // console.log(imglist)
    ctx.fillStyle = "#FFFFFF";
    ctx.fillRect(0, 0, 250, 200);
    // 商品名称
    ctx.fillStyle = "#333";
    ctx.setFontSize(18)
    var str = info.goodsName;
    if (str.length > 13) {
      str = str.substring(0, 13) + "...";
    }
    ctx.fillText(str, 10, 150)

    var img1 = this.data.img1;
    var img2 = this.data.img2;
    ctx.drawImage(img1, 10, 10, 110, 110)
    if (img2) {
      ctx.drawImage(img2, 135, 10, 110, 110)
    }
    ctx.fillStyle = "#E22405";
    ctx.setFontSize(16)
    ctx.fillText("￥", 10, 185)

    


    ctx.setFontSize(22)
    var moneywidth;
    if(type == 0){
      ctx.fillText(String(info.goodsPrice1) + '/' + info.goodsUnit, 24, 185)
      moneywidth = ctx.measureText(String(info.goodsPrice1)).width
    }else if(type == 5){
      ctx.fillText(String(info.goodsCostPrice) + '/' + info.goodsUnit, 24, 185)
      moneywidth = ctx.measureText(String(info.goodsMinPrice)).width
    }else if(type == 2){
      ctx.fillText(String(info.goodsMinPrice) + '/' + info.goodsUnit, 24, 185)
      moneywidth = ctx.measureText(String(info.goodsMinPrice)).width
    }else{
      ctx.fillText(String(info.goodsCurPrice) + '/' + info.goodsUnit, 24, 185)
      moneywidth = ctx.measureText(String(info.goodsCurPrice)).width
    }
    // // console.log(moneywidth)
    ctx.setFontSize(12)
    var discountText;
    if (type == 1 || type == 2){
      discountText = info.actDesc
    }else if(type == 0){
      discountText = '传统批发'
    }else if (type == 3) {
      discountText = '限时抢购'
    } else if (type == 4) {
      discountText = '大众拼团'
    }
    var bdColor = '#E22405';
    var bdBackground = 'transparent';
    var bdRadius = 4;
    var textPadding = 6;
    var boxHeight = 18;
    var boxWidth = ctx.measureText(discountText).width + textPadding * 2;
    ctx.fillText(discountText, moneywidth + 75, 183);
    this.roundRect(ctx, moneywidth + 70, 169, boxWidth, boxHeight, bdRadius, bdBackground, bdColor)

    

    ctx.draw()
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'shareimg',
        success(res) {
          console.log(res.tempFilePath)
          _this.setData({
            shareimg: res.tempFilePath
          })
        }
      })
    }, 500)


  },
  tokefu(){
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    // wx.showToast({
    //   title: '暂未开通服务，敬请期待',
    //   icon:'none',
    //   duration:1500
    // })
    // 跳转客服
    var _this = this;
    var rctoken = this.data.obj.shopRcToken;
    var shopid = this.data.obj.shopId;
    console.log(rctoken)
    wx.request({
      url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getRectokenInfo",
        flag: 1,   //0-普通用户 1-店铺
        // userId: rctoken,   //普通用户ID (flag为0时必传)
        shopId: shopid,    //店铺ID    (flag为1时必传)
      },
      success: function success(res) {
        console.log(res.data)
        var name = res.data.beans[0].nickName;
        var img = res.data.beans[0].photo;
        if (res.data.result == 0) {
          wx.navigateTo({
            url: '../kefu/kefu?friendId=' + rctoken + '&friendName=' + name + '&friendAvatarUrl=' + img,
          })
        }
      }
    });
  },
  tocar(){
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    wx.switchTab({
      url: '../../../pages/car/car',
    })
  },
  toshopxq(){
    var _this = this;
    wx.navigateTo({
      url: '../shopxq/shopxq?shopid='+_this.data.obj.shopId,
    })
  },
  togoodsxq(e){
    var type = e.currentTarget.dataset.type;
    var goodsid = e.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '../goodsxq/goodsxq?goodsid='+goodsid+'&type='+type,
    })
  },
  changenum(e){
    var obj = this.data.obj;
    var num = e.currentTarget.dataset.num;
    var type = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    var _this = this;
    if (type == 0) {
      if (num > 0) {
        obj.goodsSku[index].num--;
        this.setData({
          obj:obj
        })
      }
    } else {
      obj.goodsSku[index].num++;
      this.setData({
        obj: obj
      })
    }
    var list = [];
    var sublist = [];
    for (var i in obj.goodsSku) {
      if (obj.goodsSku[i].num > 0) {
        list.push(obj.goodsSku[i].skuValue1)
        sublist.push({
          goodsId: obj.goodsId,
          skuId: obj.goodsSku[i].skuId,
          operType: 0, 
          goodsBuyNum: obj.goodsSku[i].num
        })
      }
    }
    var str = '';
    for (var l in list) {
      str = str + list[l] + ','
    }
    str = str.substring(0, str.length - 1);
    this.setData({
      guigestr:str,
      sublist: sublist
    })
  },
  //店铺收藏or取消
  shopshoucang() {
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    var _this = this;
    var type = this.data.obj.shopCollect;
    if (type == 0) {
      type = 1
    } else {
      type = 0
    }

    wx.request({
      url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "collect",
        userId: wx.getStorageSync('userid'),
        type: '1', //类型   0-厂家商品收藏 1-厂家店铺收藏
        cid: _this.data.obj.shopId, //商品id或店铺id
        isCollect: type //0取消1收藏
      },
      success: function success(res) {
        console.log(res.data);
        wx.showToast({
          title: res.data.resultNote,
          icon: 'none',
          duration: 1500
        })
        if (res.data.result == 0) {
          var obj = _this.data.obj;
          obj.shopCollect = type;
          _this.setData({
            obj: obj
          })
        }
      }
    });
    
  },
  //商品收藏or取消
  goodsshoucang() {
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    var _this = this;
    var type = this.data.obj.isCollect;
    if (type == 0) {
      type = 1
    } else {
      type = 0
    }
    wx.request({
      url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "collect",
        userId: wx.getStorageSync('userid'),
        type: '0', //类型   0-厂家商品收藏 1-厂家店铺收藏
        cid: _this.data.goodsid, //商品id或店铺id
        isCollect: type //0取消1收藏
      },
      success: function success(res) {
        console.log(res.data);
        wx.showToast({
          title: res.data.resultNote,
          icon: 'none',
          duration: 1500
        })
        if (res.data.result == 0) {
          var obj = _this.data.obj;
          obj.isCollect = type;
          _this.setData({
            obj: obj
          })
        }
      }
    });
    
  },
  //本厂优选
  getyouxuan() {
    var _this = this;
    wx.request({
      url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getNgGoodsList",
        townId: wx.getStorageSync('townid'), //区县id  必传字段
        shopId: _this.data.obj.shopId,
        searchKey: "", //搜索关键字
        nowPage: 1 //非必传字段
      },
      success: function success(res) {
        console.log(res.data);
        _this.setData({
          youxuanlist: res.data.goodsList
        })
      }
    });
  },
  //发现好货
  gethaohuo() {
    var _this = this;
    wx.request({
      url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getNgGoodsList",
        townId: wx.getStorageSync('townid'), //区县id  必传字段
        searchKey: "", //搜索关键字
        nowPage: 1 //非必传字段
      },
      success: function success(res) {
        console.log(res.data);
        _this.setData({
          haohuolist: res.data.goodsList
        })
      }
    });
  },
  //获取评价
  getpingjia() {
    var _this = this;
    wx.request({
      url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getGoodsCmtList",
        goodsId: _this.data.goodsid, //商品id 
        nowPage: 1
      },
      success: function success(res) {
        console.log(res.data);
        _this.setData({
          pingjialist: res.data.commentList
        })
      }
    });
    
  },
  timeFormat: function timeFormat(param) {
    //小于10的格式化函数
    return param < 10 ? "0" + param : param;
  },
  //获取时间
  countDown() {
    var that = this;
    //倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    var newTime = new Date().getTime();
    var endTimeList = that.data.actEndTimeList;
    var countDownList = [];
    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(function (o) {
      var endTime = new Date(o).getTime();
      var obj = null;
      var time = 0;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        var _time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        var day = parseInt(_time / (60 * 60 * 24));
        var hou = parseInt(_time % (60 * 60 * 24) / 3600);
        var min = parseInt(_time % (60 * 60 * 24) % 3600 / 60);
        var sec = parseInt(_time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: that.timeFormat(day),
          hou: that.timeFormat(hou),
          min: that.timeFormat(min),
          sec: that.timeFormat(sec)
        };
        // obj = obj.day + "天" + obj.hou + ":" + obj.min + ":" + obj.sec;
      } else {
        //活动已结束，全部设置为'00'
        obj = {
          day: "00",
          hou: "00",
          min: "00",
          sec: "00"
        };
        // obj = obj.day + "天" + obj.hou + ":" + obj.min + ":" + obj.sec;
      }

      countDownList.push(obj);

      // console.log(that.data.countDownList)
    });
    that.setData({
      countDownList: countDownList
    })
    // console.log(that.data.countDownList)
    var timer = setTimeout(that.countDown, 1000);
    that.setData({
      timer:timer
    })
    //				console.log(that.countDownList)

  },
  // tosuredd(){
  //   // wx.navigateTo({
  //   //   url: '../suredd/suredd',
  //   // })
  //   var _this = this;
  //   var obj = this.data.obj;
  //   var list = this.data.sublist;
  //   var type = this.data.type;
  //   var suretype = this.data.suretype;
  //   if (list.length == 0){
  //     wx.showToast({
  //       title: '请选择想要购买的商品',
  //       icon: 'none',
  //       duration: 1500
  //     })
  //     return false;
  //   }
  //   if (!wx.getStorageSync('userid')) {
  //     wx.navigateTo({
  //       url: '../shouquan/shouquan',
  //     })
  //     return false;
  //   }
  //   list = JSON.stringify(list)
  //   if (type != 4) {
  //     if (type == 0) {
  //       var numlist = JSON.parse(list);
  //       var num = 0;
  //       for (var i in numlist) {
  //         num += Number(numlist[i].goodsBuyNum)
  //       }
  //       if (num < obj.wsNum1) {
  //         _this.closechoosebox()
  //         wx.showToast({
  //           title: '选择的数量不能低于起订量',
  //           icon:'none',
  //           duration:1500
  //         })
  //         return false;
  //       }
  //     }
  //     if(type == 5){
  //       wx.request({
  //         url: 'https://shop.zjguangxuan.com/ZJservice', //自己的服务接口地址
  //         method: "post",
  //         header: {
  //           "content-type": "application/x-www-form-urlencoded"
  //         },
  //         data: {
  //           cmd: "toAgtSaleBalance",
  //           userId: wx.getStorageSync('userid'),
  //           shopId: obj.shopId,
  //           goodsList: list
  //         },
  //         success: function success(res) {
  //           console.log(res.data);
  //           if (res.data.result == 0) {
  //             var obj = JSON.stringify(res.data);
  //             wx.navigateTo({
  //               url: '../suredd/suredd?type=5&obj=' + obj + '&goodslist=' + list,
  //             })
  //             _this.closechoosebox()
  //           } else {
  //             _this.closechoosebox()
  //             wx.showToast({
  //               title: res.data.resultNote,
  //               icon: 'none',
  //               duration: 1500
  //             })
  //           }
  //         }
  //       });
  //     }else{
  //       if(suretype == 2){
  //         wx.request({
  //           url: 'https://shop.zjguangxuan.com/ZJservice', //自己的服务接口地址
  //           method: "post",
  //           header: {
  //             "content-type": "application/x-www-form-urlencoded"
  //           },
  //           data: {
  //             cmd: "toWscBalance",
  //             userId: wx.getStorageSync('userid'),
  //             shopId: obj.shopId,
  //             goodsList: list
  //           },
  //           success: function success(res) {
  //             console.log(res.data);
  //             if (res.data.result == 0) {
  //               var obj = JSON.stringify(res.data);
  //               wx.navigateTo({
  //                 url: '../suredd/suredd?obj=' + obj + '&goodslist=' + list,
  //               })
  //               _this.closechoosebox()
  //             } else {
  //               _this.closechoosebox()
  //               wx.showToast({
  //                 title: res.data.resultNote,
  //                 icon: 'none',
  //                 duration: 1500
  //               })
  //             }
  //           }
  //         });
  //       }else{
  //         wx.request({
  //           url: 'https://shop.zjguangxuan.com/ZJservice', //自己的服务接口地址
  //           method: "post",
  //           header: {
  //             "content-type": "application/x-www-form-urlencoded"
  //           },
  //           data: {
  //             cmd: "addShopCart",
  //             flag:1,
  //             userId: wx.getStorageSync('userid'),
  //             goodsId:obj.goodsId,
  //             goodsSku: list
  //           },
  //           success: function success(res) {
  //             console.log(res.data);
  //             if (res.data.result == 0) {
  //               wx.showToast({
  //                 title: "添加成功",
  //                 icon: 'none',
  //                 duration: 1500
  //               })
  //               _this.closechoosebox()
  //             } else {
  //               wx.showToast({
  //                 title: res.data.resultNote,
  //                 icon: 'none',
  //                 duration: 1500
  //               })
  //             }
  //           }
  //         });
  //       }
        
  //     }
      
  //   }else{
  //     wx.request({
  //       url: 'https://shop.zjguangxuan.com/ZJservice', //自己的服务接口地址
  //       method: "post",
  //       header: {
  //         "content-type": "application/x-www-form-urlencoded"
  //       },
  //       data: {
  //         cmd: "toGoBalance",
  //         userId: wx.getStorageSync('userid'),
  //         shopId: obj.shopId,
  //         goodsList: list,
  //         flag: '2',
  //       },
  //       success: function success(res) {
  //         console.log(res.data);
  //         if (res.data.result == 0) {
  //           var obj = JSON.stringify(res.data);
  //           wx.navigateTo({
  //             url: '../suredd/suredd?obj=' + obj + '&goodslist=' + list+'&type=1',
  //           })
  //           _this.closechoosebox()
  //         } else {
  //           _this.closechoosebox()
  //           wx.showToast({
  //             title: res.data.resultNote,
  //             icon: 'none',
  //             duration: 1500
  //           })
  //         }
  //       }
  //     });
  //   }
  // },
  // 页面滚动事件监听
  pagescroll(e) {
    // console.log(e.detail.scrollTop + '1111')
    var _this = this;
    var index;
    const query = wx.createSelectorQuery()
    query.selectAll('#maodian1').boundingClientRect()
    query.exec(function (res) {
      if (res[0][0].top <= 200) {
        index = 1;
      }
      const query = wx.createSelectorQuery()
      query.selectAll('#maodian2').boundingClientRect()
      query.exec(function (ress) {
        if (ress[0][0].top <= 200) {
          index = 2;
        }
        const query = wx.createSelectorQuery()
        query.selectAll('#maodian3').boundingClientRect()
        query.exec(function (resss) {
          if (resss[0][0].top <= 200) {
            index = 3;
          }
          const query = wx.createSelectorQuery()
          query.selectAll('#maodian4').boundingClientRect()
          query.exec(function (ressss) {
            if (ressss[0][0].top <= 200) {
              index = 4;
            }
            _this.setData({
              tabindex: index,
            })
          })
        })
      })
    })


  },
  //弹出尺码选择窗口
  tanchusizebox(e) {
    var suretype = e.currentTarget.dataset.suretype;
    var _this = this;
    var type = this.data.type;
    if(type != 0){
      this.setData({
        blackzt: 'block',
        guigemoney: _this.data.obj.goodsSku[0].goodsCurPrice,
        suretype: suretype
      })
    }else{
      this.setData({
        blackzt: 'block',
        suretype: suretype
      })
    }
    
    setTimeout(function () {
      _this.setData({
        sizexzbox: true
      })
    }, 100)
  },
  //关闭尺码选择窗口
  closechoosebox() {
    var _this = this;
    this.setData({
      sizexzbox: false
    })
    setTimeout(function () {
      _this.setData({
        blackzt: 'none'
      })
    }, 300)

  },
  // 推荐商品tab切换锚点
  changetab(e) {
    // console.log(e.currentTarget.dataset.current)
    this.setData({
      tabindex: e.currentTarget.dataset.current,
      id: e.currentTarget.dataset.idr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  tosuredd() {
    let that = this
    wx.navigateToMiniProgram({
      appId: 'wx9184db707a7ae022',
      path: 'pages/index/index?goodsid=' + that.data.goodsid + '&type=' + that.data.type,
      envVersion: 'release',
      success(res) {
        // 打开其他小程序成功同步触发
        // wx.showToast({
        //   title: '跳转成功'
        // })
      }
    })


  },
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    var _this = this;
    var goodsid;
    var type;
    if(!options.goodsid){
      goodsid = app.globalData.morengoodsid;
    }else{
      goodsid = options.goodsid;
    }
    if (!options.type) {
      type = app.globalData.morentype;
    } else {
      type = options.type;
    }
    this.setData({
      goodsid:goodsid,
      type:type
    })
    if (!wx.getStorageSync('townid')){
      wx.getLocation({
        type: 'gcj02',
        success(res) {
          console.log(res)
          wx.request({
            url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
            method: "post",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            data: {
              cmd: 'getTownId',
              lng: res.longitude,		//地理经度
              lat: res.latitude,		//地理纬度
            },
            success: function success(ress) {
              console.log(ress.data);
              wx.setStorageSync("townid", ress.data.townId);
            }
          });
        }
      })
    }
    if (type == 0) {
      wx.request({
        url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          cmd: "getWsGoodsDetail",
          userId: wx.getStorageSync('userid'),
          goodsId: goodsid //商品id 
        },
        success: function success(res) {
          if (res.data.result == 1){
            wx.showToast({
              title: res.data.resultNote,
              icon:'none',
              duration:1500
            })
            setTimeout(function(){
              wx.navigateBack({
                delta: 1
              })
            },1500)
            return false;
          }
          console.log(res.data);
          res.data.shopScore = parseInt(res.data.shopScore * 100 / 5) + '%';
          for (var i in res.data.goodsSku) {
            res.data.goodsSku[i].num = 0;
          }
          _this.setData({
            obj:res.data
          })
          _this.getimg1(res.data.goodsImg[0])
          if (res.data.goodsImg[1]){
            _this.getimg2(res.data.goodsImg[1])
          }
          wx.hideLoading()
        }
      });
    }else if (type == 1) {
      wx.request({
        url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          cmd: "getFullSubGoodsDetail",
          userId: wx.getStorageSync('userid'),
          goodsId: goodsid //商品id 
        },
        success: function success(res) {
          if (res.data.result == 1) {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
            return false;
          }
          console.log(res.data);
          res.data.shopScore = parseInt(res.data.shopScore * 100 / 5) + '%';
          for (var i in res.data.goodsSku) {
            res.data.goodsSku[i].num = 0;
          }
          _this.setData({
            obj: res.data
          })
          _this.getimg1(res.data.goodsImg[0])
          if (res.data.goodsImg[1]) {
            _this.getimg2(res.data.goodsImg[1])
          }
          wx.hideLoading()
        }
      });
    } else if (type == 2) {
      wx.request({
        url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          cmd: "getFullGiveGoodsDetail",
          userId: wx.getStorageSync('userid'),
          goodsId: goodsid //商品id 
        },
        success: function success(res) {
          console.log(res.data);
          if (res.data.result == 1) {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
            return false;
          }
          
          res.data.shopScore = parseInt(res.data.shopScore * 100 / 5) + '%';
          for (var i in res.data.goodsSku) {
            res.data.goodsSku[i].num = 0;
          }
          _this.setData({
            obj: res.data
          })
          _this.getimg1(res.data.goodsImg[0])
          if (res.data.goodsImg[1]) {
            _this.getimg2(res.data.goodsImg[1])
          }
          wx.hideLoading()
        }
      });
    } else if (type == 3) {
      wx.request({
        url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          cmd: "getDisctGoodsDetail",
          userId: wx.getStorageSync('userid'),
          goodsId: goodsid //商品id 
        },
        success: function success(res) {
          if (res.data.result == 1) {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
            return false;
          }
          console.log(res.data);

          var endTimeList = [];
          var str = res.data.actEndTime.replace(/-/g, "/");
          endTimeList.push(str);
          res.data.shopScore = parseInt(res.data.shopScore * 100 / 5) + '%';
          for (var i in res.data.goodsSku) {
            res.data.goodsSku[i].num = 0;
          }
          _this.setData({
            obj: res.data,
            actEndTimeList: endTimeList
          })
          _this.getimg1(res.data.goodsImg[0])
          if (res.data.goodsImg[1]) {
            _this.getimg2(res.data.goodsImg[1])
          }
          wx.hideLoading()
          //开始倒计时
          _this.countDown();
          
        }
      });
    } else if (type == 4) {
      wx.request({
        url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          cmd: "getGoGoodsDetail",
          userId: wx.getStorageSync('userid'),
          goodsId: goodsid //商品id 
        },
        success: function success(res) {
          console.log(res.data);
          if (res.data.result == 1) {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
            return false;
          }
          var endTimeList = [];
          var str = res.data.goEndTime.replace(/-/g, "/");
          endTimeList.push(str);
          res.data.shopScore = parseInt(res.data.shopScore * 100 / 5) + '%';
          for (var i in res.data.goodsSku) {
            res.data.goodsSku[i].num = 0;
          }
          _this.setData({
            obj: res.data,
            actEndTimeList: endTimeList
          })
          _this.getimg1(res.data.goodsImg[0])
          if (res.data.goodsImg[1]) {
            _this.getimg2(res.data.goodsImg[1])
          }
          wx.hideLoading()
          //开始倒计时
          _this.countDown();
        }
      });
    } else if (type == 5) {
      wx.request({
        url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          cmd: "getTourGoodsDetail",
          userId: wx.getStorageSync('userid'),
          goodsId: goodsid //商品id 
        },
        success: function success(res) {
          console.log(res.data);
          if (res.data.result == 1) {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
            return false;
          }
          console.log(res.data);
          res.data.shopScore = parseInt(res.data.shopScore * 100 / 5) + '%';
          for (var i in res.data.goodsSku) {
            res.data.goodsSku[i].num = 0;
          }
          _this.setData({
            obj: res.data
          })
          _this.getimg1(res.data.goodsImg[0])
          if (res.data.goodsImg[1]) {
            _this.getimg2(res.data.goodsImg[1])
          }
          wx.hideLoading()
          // var endTimeList = [];
          // var str = res.data.actEndTime.replace(/-/g, "/");
          // endTimeList.push(str);
          // res.data.shopScore = parseInt(res.data.shopScore * 100 / 5) + '%';
          // for (var i in res.data.goodsSku) {
          //   res.data.goodsSku[i].num = 0;
          // }
          // _this.setData({
          //   obj: res.data,
          //   actEndTimeList: endTimeList
          // })
          // _this.getimg1(res.data.goodsImg[0])
          // if (res.data.goodsImg[1]) {
          //   _this.getimg2(res.data.goodsImg[1])
          // }
          // wx.hideLoading()
          // //开始倒计时
          // _this.countDown();
        }
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    this.getpingjia();
    wx.hideHomeButton({
      success:function(res){
        
      }
    })
    if (this.data.countDownList.length >0){
      _this.countDown();
    }
    var timer = setInterval(function(){
      if (_this.data.obj.shopId && wx.getStorageSync('townid')){
        _this.gethaohuo();
        _this.getyouxuan();
        clearInterval(timer)
      }
    },100)
    
    var timerr = setInterval(function () {
      if(_this.data.type == 5){
        clearInterval(timerr)
      }else{
        if (_this.data.img1) {
          _this.getshareimg();
          clearInterval(timerr)
        }
      }
      
    }, 100)
    
    wx.getSystemInfo({
      success(res) {
        console.log(res)
        _this.setData({
          pageheight: (res.windowHeight * 2) / ((res.windowWidth * 2) / 750) - 200,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var timer = this.data.timer;
    clearTimeout(timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var timer = this.data.timer;
    clearTimeout(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _this = this;
    var obj = this.data.obj;
    var type = this.data.type;
    var shareimg = this.data.shareimg;
    return {
      path: "/pages/goodsxq/goodsxq?goodsid=" + obj.goodsId + '&type=' + type,
      imageUrl: shareimg,
      title: obj.goodsName,
    };
  }
})